<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 搜索框组件 -->
    <Search :searchData="pagination" @handle-clear="handleClear" @handle-reset="handleReset"
      @handle-search="handleSearch"></Search>

    <!-- 表格引入 -->
    <TableList :list-data="data" :pagination="pagination" :total="pagination.total" @handle-edit="handleEdit"
      @handle-bulid="handleBulid" @handle-click-delete="handleClickDelete" @handle-forbidden="handleForbidden"
      @getCurrent="getCurrent"></TableList>

    <!-- 新增或者编辑对话框 -->
    <DialogForm :visible="visible" ref="formRef" :title="title" :data="formBaseData" :planData="planData"
      @handle-close="handleClose" @handle-add="handleAdd" @handleEditForm="editProject">
    </DialogForm>

    <!-- 删除弹窗 -->
    <DeleteForm :visible="dialogDeleteVisible" :delete-text="operateText" @handle-delete="handleDelete"
      @handle-close="handleDeleteClose"></DeleteForm>

    <!-- 禁用弹窗 -->
    <ForbiddenForm :visible="dialogVisible" :text="operateText" @handle-submit="handleForbiddenSub"
      @handle-close="handleForbiddenClose"></ForbiddenForm>
  </div>
</template>

<script setup lang="ts">
/* 搜索框引入 */
import { onMounted, ref } from 'vue';
import DialogForm from '@/pages/serve/plan/grade/components/DialogFrom.vue';
import Search from '@/pages/serve/plan/grade/components/SearchForm.vue';
import TableList from '@/pages/serve/plan/grade/components/TableList.vue';

// 删除弹层
import DeleteForm from '@/components/OperateDialog/index.vue'
// 禁用弹窗
import ForbiddenForm from '@/components/Forbidden/index.vue'
import { getLevelAllList, getLevelDetails, getLevelList, getnursAllPlanList, levelAdd, levelDelete, levelStatus, levelUpdate } from '@/api/serve';
import { MessagePlugin } from 'tdesign-vue-next';


//分页对象
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  name: '',
  status: ''
});

/* 表格的列表数据 */
const data = ref([]);
const visible = ref(false) // 新增，编辑弹窗
const formBaseData = ref({}) // 弹窗表单内容
const title = ref('') // 弹窗标题
const dialogDeleteVisible = ref(false) // 控制删除弹层显示隐藏
const dialogVisible = ref(false) // 禁用弹窗
const typeId = ref('') // 设置删除id
const typeStatus = ref(null) // 禁用启用
const operateText = ref('护理等级') // 要操作的内容提示
const statusText = ref('') // 启用禁用提示
//const total = ref(0) // 总条数
const planData = ref([]) // 护理计划数据
const formRef = ref(null)
/* ------------------------------------------------------------------------------------------- */
//重置搜索框
const handleReset = () => {
  //重置页码
  pagination.value = {
    pageNum: 1,
    pageSize: 10,
    total: 0,
    name: '',
    status: ''
  }
  searchProject();
}

//搜索
const handleSearch = () => {
  //点击了搜索按钮之后，就需要重置页码
  pagination.value.pageNum = 1;
  searchProject();
}

//清空搜索项
const handleClear = (val) => {
  if (val === 'name') {
    delete pagination.value.name;
  } else {
    delete pagination.value.status;
  }
  searchProject();
}

/* 条件搜索护理项目 */
const searchProject = async () => {
  let result: any = await getLevelList(pagination.value);
  pagination.value.total = result.data.total;//总条数
  data.value = result.data.records;
};

// 翻页设置当前页
const getCurrent = (val) => {
  pagination.value.pageNum = val.current
  pagination.value.pageSize = val.pageSize
  searchProject()
}

const handleEdit = async (val) => {
  //请求后端
  let result: any = await getLevelDetails(val.id);
  if (result.code === 200) {
    //父传子 给表单数据赋值
    formBaseData.value = result.data;
    //显示弹框
    title.value = '编辑';
    visible.value = true;
  }
}

// 添加
const handleAdd = async (val) => {
  const res: any = await levelAdd(val)
  if (res.code === 200) {
    MessagePlugin.success('添加成功')
    searchProject();
    handleClose()
    formRef.value.handleClear()
  }
}

// 打开新增表单
const handleBulid = () => {
  title.value = '新增';
  visible.value = true;
}
//打开删除弹窗
const handleClickDelete = (val) => {
  typeId.value = val.id
  dialogDeleteVisible.value = true
}

// 确认删除
const handleDelete = async () => {
  const res = await levelDelete(typeId.value)
  if (res.code === 200) {
    dialogDeleteVisible.value = false;
    MessagePlugin.success('删除成功');
    // 获取当前页和总页数
    if (pagination.value.pageNum > 1 && data.value.length === 1) {
      pagination.value.pageNum -= 1;
    }
    searchProject();
  }
}
// 关闭删除弹窗
const handleDeleteClose = () => {
  dialogDeleteVisible.value = false
}

const handleForbidden = (val) => {
  typeId.value = val.id
  if (val.status === 1) {
    dialogVisible.value = true
    typeStatus.value = 0
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1
    handleForbiddenSub();
    statusText.value = '启用成功'
  }
}

// 确定禁用
const handleForbiddenSub = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const res: any = await levelStatus(params)
  if (res.code === 200) {
    dialogVisible.value = false
    MessagePlugin.success(statusText.value);
    searchProject();
  }
}
// 关闭禁用弹窗
const handleForbiddenClose = () => {
  dialogVisible.value = false
}


// 查询所有护理计划(下拉框)
const getAllLevel = async () => {
  let result: any = await getnursAllPlanList();
  if (result.code === 200) {
    planData.value = result.data;
  }
}


const editProject = async (project) => {
  /* 请求后端 */
  let result: any = await levelUpdate(project);
  if (result.code === 200) {
    MessagePlugin.success('修改成功');
    // 关闭窗口
    handleClose();
    //重新查询
    searchProject();
    //表单数据置空
    formRef.value.handleClear();
  } else {
    MessagePlugin.error(result.msg);
  }
}
// 关闭弹窗
const handleClose = () => {
  visible.value = false // 关闭新增弹窗
  getAllLevel();
  searchProject();
}

// 钩子函数
onMounted(() => {
  getAllLevel();
  searchProject();
})
</script>